<?php

namespace app\controller;

use think\facade\Request;
use \think\facade\Db;
use app\BaseController;
use think\facade\View;


/**
 * 获取指定问卷及其问题的数据
 *
 * @param int $surveyId 问卷ID
 * @return array 包含问卷及问题数据的关联数组
 */
class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V6<br/><span style="font-size:30px">13载初心不改 - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

    public function check(Request $request)
    {
        // 获取 POST 数据
        $data = $request->post();

        // 业务逻辑
        $userid = $data['username'];
        $userpassword = $data['userpassword'];
        // 查询数据库中的用户密码
        $db_user = Db::table('userpassword')->where('id', $userid)->find();

        if ($db_user) {
            // 检查用户密码是否匹配
            if ($userpassword === $db_user['userpassword']) {
                return json(['status' => true, 'message' => 'Login successful']);
            } else {
                return json(['status' => false, 'message' => 'Incorrect password']);
            }
        } else {
            // 用户名不存在
            return json(['status' => 'fail', 'message' => 'Username not found']);
        }
    }
    public function getAllBaidubaikeData()
    {
        try {
            // 查询 userwaitdata 表中的所有数据
            $userWaitData = Db::name('baidubaike')->select();

            // 返回查询结果
            return json([
                'status' => true,
                'message' => '查询成功',
                'data' => $userWaitData
            ]);
        } catch (\Exception $e) {
            // 异常处理
            return json([
                'status' => false,
                'message' => $e->getMessage()
            ]);
        }
    }
    public function getAllBaidubaikeDataById()
    {
        $data = Request::post();
        $input_id = $data['id'];
        try {
            // 查询 userwaitdata 表中的所有数据
            $userWaitData = Db::name('baidubaike')->where('id', $input_id)->select();
            // 返回查询结果
            return json([
                'status' => true,
                'message' => '查询成功',
                'data' => $userWaitData
            ]);
        } catch (\Exception $e) {
            // 异常处理
            return json([
                'status' => false,
                'message' => $e->getMessage()
            ]);
        }
    }
    public function getSurveyById()
    {
        $data = Request::post();
        $surveyId = $data['id'];
        try {
            // 查询问卷及其问题
            $survey = Db::name('surveys')
                ->where('id', $surveyId)
                ->find();

            if ($survey) {
                $questions = Db::name('questions')
                    ->where('survey_id', $surveyId)
                    ->select();

                $survey['questions'] = $questions;

                // 返回 JSON 格式的数据
                return json([
                    'status' => true,
                    'message' => '获取问卷成功',
                    'data' => $survey
                ]);
            } else {
                return json([
                    'status' => false,
                    'message' => '问卷不存在',
                    'data' => null
                ]);
            }
        } catch (\Exception $e) {
            // 捕获异常，返回错误信息
            return json([
                'status' => false,
                'message' => '获取问卷失败：' . $e->getMessage(),
                'data' => null
            ]);
        }
    }
    public function getAllSurvey()
    {
        try {
            // 查询 userwaitdata 表中的所有数据
            $userData = Db::name('surveys')->select();

            // 返回查询结果
            return json([
                'status' => true, 'message' => '查询成功',
                'data' => $userData
            ]);
        } catch (\Exception $e) {
            // 异常处理
            return json([
                'status' => false,
                'message' => $e->getMessage()
            ]);
        }
    }
}
